package com.capgemini.cn.authority.data.repository;

import com.capgemini.cn.authority.data.entity.Authorities;
import com.capgemini.cn.authority.data.entity.AuthoritiesType;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Lock;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import javax.persistence.LockModeType;
import java.util.List;

/**
 * Created at 2017/8/24
 *
 * @author Andriy
 */
@Repository
public interface AuthoritiesRepository extends JpaRepository<Authorities, Long> {

    /**
     * 查询所有指定ID的权限信息
     *
     * @param ids 需要查询的权限信息主键ID值
     */
    List<Authorities> findAllByIdIn(Long[] ids);

    /**
     * 根据权限编码查询权限信息
     *
     * @param authorityCode 权限编码
     */
    Authorities findByAuthorityCode(@Param(value = "authorityCode") String authorityCode);

    /**
     * 根据权限编码查询权限信息 - <span style='color:yellow'>使用悲观写锁控制数据脏读</span>
     *
     * @param authorityCode 权限编码
     */
    @Lock(value = LockModeType.PESSIMISTIC_WRITE)
    Authorities findByAuthorityCodeEquals(@Param(value = "authorityCode") String authorityCode);

    /**
     * 查询指定权限类型且不包含对应权限内容的权限列表信息 - <span style='color:yellow'>使用悲观写锁控制数据脏读</span>
     *
     * @param systemCreated   是否系统自动创建
     * @param authoritiesType 指定权限类型
     * @param authorityCode   排除在外的权限编码列表信息
     */
    @Lock(value = LockModeType.PESSIMISTIC_WRITE)
    List<Authorities> findBySystemCreatedAndAuthoritiesTypeAndAuthorityCodeNotIn(boolean systemCreated, AuthoritiesType authoritiesType, String[] authorityCode);
}
